<?php
class Model_Person extends Model_Template
{
	public function __construct()
	{
		parent::__construct();
		$this->className = 'Model_Table_Person';

	}
	
	///////////////////////////////////////////////////////return a person with a phone number
	public function getPerson($phoneNumber)
	{
		//Select one person query
		$sqlSelectPerson = 'SELECT * FROM person WHERE home_phone_number= ? OR cell_phone_number= ?;';
		
		//Prepare statement
		$selectPerson = Controller_Template::$db->prepareEKADBPDO($sqlSelectPerson, $this->className, array('person_id','first_name','last_name','home_phone_number','email','role','gender','cell_phone_number','cell_phone_carrier'));
		//Execute statement
		$selectPerson->execute(array($phoneNumber,$phoneNumber));
		
		return $selectPerson->fetch();
		
	}
	
	
	//////////////////////////////////////////////////////////return a persin by person_id
	public function getPersonById($personId)
	{
		//Select one person query
		$sqlSelectPerson = 'SELECT * FROM person WHERE person_id= ?;';
	
		//Prepare statement
		$selectPerson = Controller_Template::$db->prepareEKADBPDO($sqlSelectPerson, $this->className, array('person_id','first_name','last_name','home_phone_number','email','role','gender','cell_phone_number','cell_phone_carrier'));
		//Execute statement
		$selectPerson->execute(array($personId));
	
		return $selectPerson->fetch();
		
	
	}
	
	
	////////////////////////////////////////////////////create a person
	public function createNewPerson($newP)
	{
		//Create new person query
		$sqlInsertPerson = 'INSERT INTO person (first_name,last_name,home_phone_number,email,role,gender,cell_phone_number,cell_phone_carrier) VALUES (?,?,?,?,?,?,?,?);';
		
		//Prepare statement
		$insertPerson = Controller_Template::$db->prepareEKADBPDO($sqlInsertPerson, $this->className, array('person_id','first_name','last_name','home_phone_number','email','role','gender','cell_phone_number','cell_phone_carrier'));
		
		//Execute statement
		$insertPerson->execute(array($newP[':first_name'],$newP[':last_name'],$newP[':home_phone_number'],$newP[':email'],$newP[':role'],$newP[':gender'],$newP[':cell_phone_number'],$newP[':cell_phone_carrier']));
		
		if($insertPerson->rowCount()==1)
		{
			return Controller_Template::$db->lastInsertId();
		}
	}
	
	
	/////////////////////////////////////////////////////delete a person
	public function deletePerson($targetPerson)
	{
		//Delete person query
		$sqlDeletePerson = 'DELETE FROM  person WHERE person_id=?;';
		
		//Prepare statement
		$deletePerson = Controller_Template::$db->prepareEKADBPDO($sqlDeletePerson, $this->className, array('person_id','first_name','last_name','home_phone_number','email','role','gender','cell_phone_number','cell_phone_carrier'));
		
		//Execute statement
		$deletePerson->execute(array($targetPerson));
		
		return $deletePerson->rowCount();
	}
	
	///////////////////////////////////////////////////////////update a person
	public function updatePerson($targetPerson)
	{
		//update person query
		$sqlUpdatePerson = 'UPDATE person SET first_name =?,last_name=?,home_phone_number=?,email=?,role=?,gender=?,cell_phone_number=?,cell_phone_carrier=? WHERE person_id=?;';
	
		//Prepare statement
		$updatePerson = Controller_Template::$db->prepareEKADBPDO($sqlUpdatePerson, $this->className, array('person_id','first_name','last_name','home_phone_number','email','role','gender','cell_phone_number','cell_phone_carrier'));
	
		//Execute statement
		$updatePerson->execute(array($targetPerson[':first_name'],$targetPerson[':last_name'],$targetPerson[':home_phone_number'],$targetPerson[':email'],$targetPerson[':role'],$targetPerson[':gender'],$targetPerson[':cell_phone_number'],$targetPerson[':cell_phone_carrier'],$targetPerson[':person_id']));
	
		return $updatePerson->rowCount();
	}
	
}